package com.kinghunt.misp.web.controller.devicex;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceRepairDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.param.devicex.DeviceInspectionParam;
import com.kinghunt.misp.common.param.devicex.DeviceRepairParam;
import com.kinghunt.misp.service.api.devicex.DeviceRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 设备维修管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/deviceRepair/")
public class DeviceRepairController {

    @Autowired(required = false)
    private DeviceRepairService deviceRepairService;

    /**
     * 列表显示：显示设备维修列表
     */
    @GetMapping("getDeviceRepairList")
    public ApiResult<PageInfo<DeviceRepairDTO>> getDeviceRepairList(Long deviceId, int pageNum, int pageSize) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入耗材信息！");
        }
        DeviceRepairParam deviceRepairParam = new DeviceRepairParam();
        deviceRepairParam.setDeviceId(deviceId);
        deviceRepairParam.setPageNum(pageNum);
        deviceRepairParam.setPageSize(pageSize);
        //UserDTO loginUser = getCurrentUser();
        return deviceRepairService.getDeviceRepairList(deviceRepairParam);
    }
    /**
     * 设备维修详情展示
     */
    @GetMapping("getDeviceRepairDetail")
    public ApiResult<DeviceRepairDTO> getDeviceRepairDetail(Long deviceRepairId) {
        if(Objects.isNull(deviceRepairId)){
            return ApiResult.failOfMessage("查询失败，请输入设备维修信息！");
        }
        return deviceRepairService.getDeviceRepairDetail(deviceRepairId);
    }
    /**
     * 新增设备维修
     */
    @PostMapping(value = "addDeviceRepair", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addDeviceRepair(@Valid @RequestBody DeviceRepairParam deviceRepairParam) {
        if(Objects.isNull(deviceRepairParam.getDeviceId())){
            return ApiResult.failOfMessage("新增失败，请输入设备维修信息！");
        }
        UserDTO loginUser = getCurrentUser();
        deviceRepairParam.setCreatorId(loginUser.getId());
        return deviceRepairService.addDeviceRepair(deviceRepairParam);
    }
}
